<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="isCt">
        <el-select placeholder="请选择类型" v-model="queryParams.isCt" @keyup.enter.native="handleQuery">
          <el-option label="采购单" value="1" />
          <el-option label="采退单" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="purchaseList">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column
        label="单号"
        align="center"
        prop="purchaseCode"
        width="200"
      />
      <el-table-column label="供应商" align="center" prop="supplyName" />
      <el-table-column label="总数量" align="center" prop="znum" />
      <el-table-column label="总金额" align="center" prop="zprice" />

     <el-table-column label="类型" align="center" prop="isCt">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isCt === '1'">采购单</el-tag>
          <el-tag  v-if="scope.row.isCt === '2'">采退单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSh(scope.row)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
       <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采销对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
      style="margin-left: 180px"
    >
      <el-table
        v-loading="loading"
        :data="xqpurchaseList"
        show-summary
        :summary-method="getSummaries"
      >
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column label="商品码" align="center" prop="productcode" />
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="规格" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="sum" />
        <el-table-column label="价格" align="center" prop="price" />
        <el-table-column label="总金额" align="center" prop="zprice" />
        <el-table-column
          label="生产日期"
          align="center"
          prop="manufactureDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.manufactureDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="过期日期"
          align="center"
          prop="expirationDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.expirationDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center" prop="productBrand" />
      </el-table>

      <el-form ref="form" :model="form" style="margin-top: 25px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核意见" label-width="80px">
              <el-input
                v-model="form.shRemark"
                type="textarea"
                placeholder="请输入审核意见"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="audit('0')" v-hasPermi="['supermarket:purchase:audit']">审核通过</el-button>
        <el-button type="primary" @click="audit('2')" v-hasPermi="['supermarket:purchase:audit']">审核不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPurchase,
  getPurchase,
  delPurchase,
  addPurchase,
  updatePurchase,
  ark,
  auditPurchase,
} from "@/api/supermarket/purchase";
import {
  listXqpurchase,
  getXqpurchase,
  delXqpurchase,
  addXqpurchase,
  updateXqpurchase,
} from "@/api/supermarket/xqpurchase";
export default {
  name: "CxTemp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      purchaseId: "",
      // 采销表格数据
      purchaseList: [],
      xqpurchaseList: [],
      // 弹出层标题
      title: "",
      cid: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseCode: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        isCt: null,
        isZf: null,
        isRk: null,
        isSh: 1,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //汇总展示
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then((response) => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //详情
    getListXq(row) {
      const data = {
        purchaseId: row.purchaseId,
      };
      listXqpurchase(data).then((response) => {
        this.xqpurchaseList = response.data;
      });
    },
    //审核弹框
    handleSh(row) {
      this.title =
        "详情：" +
        row.purchaseCode +
        ">创建人:" +
        row.createName +
        ">供应商:" +
        row.supplyName;
      this.getListXq(row);
      this.purchaseId = row.purchaseId;
      this.open = true;
    },
    //审核
    audit(isSh) {
      const data = {
        isSh: isSh,
        purchaseId: this.purchaseId,
        shRemark: this.form.shRemark,
      };
      auditPurchase(data).then((res) => {
        if (isSh === "0") {
          this.$modal.msgSuccess("审核通过！");
        } else {
          this.$modal.msgError("审核不通过！");
        }
        this.open = false;
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        cxId: null,
        supplyId: null,
        zsum: null,
        zprice: null,
        isSh: null,
        createName: null,
        shRemark: null,
        shTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index === 5) {
          sums[index] = "";
          return;
        }
        if (index === 9) {
          sums[index] = "";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
  },
};
</script>
